import { Utils } from './../../common/utils';
import { LoadingUtil } from './../../common/loading';
import { BusiserviceProvider } from './../../providers/busiservice/busiservice';
import { UiserviceProvider } from './../../providers/uiservice/uiservice';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';

/**
 * Generated class for the InfomodifyPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-infomodify',
  templateUrl: 'infomodify.html',
})
export class InfomodifyPage {
  user:any={
    name:'',
    phone:'',
    address:''
  }
  loading:LoadingUtil;
  constructor(public navCtrl: NavController, public navParams: NavParams,
    public busiservice: BusiserviceProvider, private loadingCtrl: LoadingController, private uiservice: UiserviceProvider) {
      this.loading = new LoadingUtil(this.loadingCtrl);
      this.getuserinfo();
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad InfomodifyPage');
  }
  getuserinfo(){
      this.loading.showLoading();
      this.busiservice.getUserinfo().subscribe(
        data => {
          if (data._success) {
            this.user = data.result;
            this.loading.hideLoading();
            //用户id
          } else {
            this.loading.hideLoading();
            this.uiservice.showAlert(data._message);
          }
        }
        ,
        error => {
          this.loading.hideLoading();
          this.uiservice.showAlert("网络异常!");
        }
      );
    }
    save(){
      if(Utils.isNullOrEmpty(this.user.name)){
        alert("名称为空!");
        return;
      }
      if(Utils.isNullOrEmpty(this.user.phone)){
        alert("电话为空!");
        return;
      }
      if(Utils.isNullOrEmpty(this.user.address)){
        alert("地址为空!");
        return;
      }
      this.loading.showLoading();
      this.busiservice.updateuser(this.user).subscribe(
        data => {
          if (data._success) {
            this.loading.hideLoading();
            this.uiservice.showToast("保存成功!");
            this.navCtrl.pop();
            //用户id
          } else {
            this.loading.hideLoading();
            this.uiservice.showAlert(data._message);
          }
        }
        ,
        error => {
          this.loading.hideLoading();
          this.uiservice.showAlert("网络异常!");
        }
      );
    }
  }

